package org.example.rowing.controller.util;

import javafx.animation.FadeTransition;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Duration;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

@Slf4j
public abstract class BaseController {

    @FXML
    public void initialize() {
        Button button = getButton();
        if (button != null) {
            button.getStyleClass().add("selected");
        }
    }

    // 抽象方法，子类必须实现并提供具体的 button
    protected abstract Button getButton();

    public Optional<ButtonType> showConfirmationDialog() {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("确认删除");
        alert.setHeaderText("您确定要删除该项吗？");
        alert.setContentText("删除后将无法恢复");

        DialogPane dialogPane = alert.getDialogPane();

        // 加载CSS样式表
        dialogPane.getStylesheets().add(getClass().getResource("/css/dialog.css").toExternalForm());
        dialogPane.getStyleClass().add("confirmation-dialog");

        // 添加自定义图标
        ImageView icon = new ImageView(new Image(getClass().getResourceAsStream("/image/warning.png")));
        icon.setFitWidth(48);
        icon.setFitHeight(48);
        dialogPane.setGraphic(icon);

        // 自定义按钮文本与样式
        ButtonType confirmType = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        ButtonType cancelType = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);
        alert.getButtonTypes().setAll(confirmType, cancelType);

        // 按钮悬停效果
        Button confirmButton = (Button) dialogPane.lookupButton(confirmType);
        confirmButton.getStyleClass().add("confirm-button");
        Button cancelButton = (Button) dialogPane.lookupButton(cancelType);
        cancelButton.getStyleClass().add("cancel-button");

        // 淡入动画
        FadeTransition fadeIn = new FadeTransition(Duration.millis(300), dialogPane);
        fadeIn.setFromValue(0);
        fadeIn.setToValue(1);
        alert.setOnShown(e -> fadeIn.play());

        return alert.showAndWait();
    }
}

